package service

import (
	"go-demo/dao"
	"go-demo/model"
)

func GetCommentList(comments []model.Comment) (commentList []model.Comment) {
	var comment model.Comment
	for k := range comments {
		user, _ := dao.GetUserByID(comments[k].UserId)
		//拷贝自身属性
		comment.Id = comments[k].Id
		comment.Content = comments[k].Content
		comment.CreateDate = comments[k].CreateDate
		//拷贝用户属性
		comment.User.Id = user.Id
		comment.User.Name = user.Name
		comment.User.FollowCount = user.FollowCount
		comment.User.FollowerCount = user.FollowerCount
		comment.User.IsFollow = user.IsFollow
		comment.User.Avatar = user.Avatar
		comment.User.BackgroundImage = user.BackgroundImage
		comment.User.Signature = user.Signature
		comment.User.TotalFavourite = user.TotalFavourite
		comment.User.WorkCount = user.WorkCount
		comment.User.FavoriteCount = user.FavoriteCount
		commentList = append(commentList, comment)
	}
	return commentList
}

func CreateComment(user model.User, videoId int64, text string) model.Comment {
	//将评论填入评论表
	dao.CreateComment(user.Id, videoId, text)
	//返回评论类型
	//用用户id和视频id查询返回评论
	comment, _ := dao.GetCommentByUserIDAndVideoId(user.Id, videoId)
	//commentDate := time.Time{dao.Comment{CreateDate}: comment.CreateDate.Format("01-02")}
	commentFormat := model.Comment{Id: comment.Id, User: user, Content: comment.Content, CreateDate: comment.CreateDate}
	//createDate, _ := time.Parse("2006-01-02T15:04:05.000Z", comment.CreateDate)
	//formattedCreateDate := createDate.Format("01-02")
	return commentFormat
}

func DeleteCommentByID(commentId int64) {
	dao.DeleteCommentByID(commentId)
}

func CommentList(videoId int64) []model.Comment {
	//通过视频id查评论列表
	comments, _ := dao.GetCommentByVideoID(videoId)
	commentList := GetCommentList(comments)
	return commentList
}
